<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\TaskItem;

/**
 * TaskItemSearch represents the model behind the search form about `app\models\TaskItem`.
 */
class TaskItemSearch extends TaskItem
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'task_id', 'type', 'status', 'visit_cnt'], 'integer'],
            [['url', 'arg', 'method', 'lang', 'host', 'user_agent', 'create_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = TaskItem::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'task_id' => $this->task_id,
            'type' => $this->type,
            'status' => $this->status,
            'visit_cnt' => $this->visit_cnt,
            'create_time' => $this->create_time,
        ]);

        $query->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'arg', $this->arg])
            ->andFilterWhere(['like', 'method', $this->method])
            ->andFilterWhere(['like', 'lang', $this->lang])
            ->andFilterWhere(['like', 'host', $this->host])
            ->andFilterWhere(['like', 'user_agent', $this->user_agent]);

        return $dataProvider;
    }
}
